<?php

namespace App\Console\Commands;

use App\Models\Combination;
use Illuminate\Console\Command;

class GroupCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Group';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //每分钟查看一次拼团数据库，然后取出结束时间
        $res = Combination::get();
        //判断团的结束时间，如果结束时间等于当前时间的话，就判断参团用户和拼团用户，如果参团用户满了则拼团成功，将状态进行修改
        //如果参团用户小于拼团人数说明团未满但是时间到了，我们将拼团状态改为拼团失败
        if($res['stop_time']==date('Y-m-d H:i:s',time())){
            if($res['people']==$res['group_number']){
                Combination::where('id',$res['id'])->update(['state'=>2]);
            }elseif ($res['people']<$res['group_number']){
                Combination::where('id',$res['id'])->update(['state'=>3]);
            }
        }
        //这个是在时间未到的情况下，参团人数和拼团人数一致我们将更改状态未拼团成功
        if($res['people']==$res['group_number']){
            Combination::where('id',$res['id'])->update(['state'=>2]);
        }
    }
}
